﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Ppt = Microsoft.Office.Interop.PowerPoint;
using DataFactory;

namespace ObjectModel.PowerPoint.New
{
    public class PresentationConstants
    {
        readonly PPTSerializer mSerializer = new PPTSerializer();

        public ObservableCollection<Constant> Constants { get; set; }

        public PresentationConstants(Ppt.Presentation pres)
        {
            // If presentation contains stored XML data, deserialize.
            if (PPTSerializer.ContainsXMLParts(pres, "ArrayOfConstant"))
                Constants = PPTSerializer.DeSerializeAll<ObservableCollection<Constant>>(pres, "ArrayOfConstant")[0];
            else
                Constants = new ObservableCollection<Constant>();

            PresentationEvents events = Globals.PresentationEvents[pres];
            events.PresentationBeforeSave += OnPresentationBeforeSave;
        }

        void OnPresentationBeforeSave(object sender, PresentationBeforeSaveEventArgs e)
        {
            if (Constants.Count > 0)
                mSerializer.SerializeToPresentation(e.Presentation, Constants);
        }
    }
}
